package com.example.lt.controller.admin;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.lt.authority.Authority;
import com.example.lt.context.BaseContext;
import com.example.lt.entity.user.Permission;
import com.example.lt.service.user.PermissionService;
import com.example.lt.util.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 小涛Tao
 * @Date: 2024/08/16/19:11
 * version 1.0 注释：权限管理控制层
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/authorize/permission")
public class PermissionController {

    private final PermissionService permissionService;

    /**
     * 获取权限列表
     * @return
     */
    @GetMapping("/list")
    public List<Permission> list() {
        log.info("获取权限列表");
        return permissionService.list(null);
    }

    /**
     * 新增权限时树形结构
     * @return
     */
    @GetMapping("/treeSelect")
    public List<Permission> treeSelect() {
        log.info("新增权限时获取树形结构");
        return permissionService.treeSelect();
    }

    /**
     * 添加权限
     * @param permission
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Permission permission){
        log.info("添加权限");
        permission.setIcon("fa "+permission.getIcon());
        permissionService.save(permission);

        return Result.ok();
    }

    /**
     * 修改权限
     * @param permission
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Permission permission){
        log.info("修改权限");
        permission.setIcon("fa "+permission.getIcon());
        permissionService.updateById(permission);
        return Result.ok();
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        log.info("删除权限");
        permissionService.removeMenu(id);
        return Result.ok().message("删除成功");
    }


    /**
     * 初始化菜单
     * @return
     */
    @GetMapping("/initMenu")
    public Map<String, Object> initMenu(){
        Map<String, Object> data = permissionService.initMenu(BaseContext.get());
        return data;
    }

}
